package com.jie.web.controller;


import com.jie.dto.UserInfoFriendDTO;
import com.jie.mapper.FriendImMapper;
import com.jie.service.FriendImService;
import com.jie.util.UserUtils;
import com.jie.vo.CommonResponse;
import com.jie.dto.FriendDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xiaojie
 * @since 2023-11-01
 */
@RestController
@RequestMapping("/friend-im")
@Api("好友管理")
public class FriendImController {

    //获取好友列表
    @Autowired
    private FriendImService friendService;
    @Autowired
    private FriendImMapper friendImMapper;

    @GetMapping("/list")
    @ApiOperation(value = "好友列表",notes="获取好友列表")
    public CommonResponse<List<FriendDTO>> findFriends(){
        List<FriendDTO> friends = friendService.findFriendByUserId(UserUtils.getLoginUser().getUsername());
        return CommonResponse.buildSuccess(friends);
    }
    @PostMapping("/addFriend/{friendId}")
    @ApiOperation(value = "添加好友",notes="双方建立好友关系")
    public CommonResponse addFriend(@NotEmpty(message = "好友id不可为空") @PathVariable("friendId") Long friendId){
        friendService.addFriend(friendId);
        return CommonResponse.buildSuccess("申请成功，等待对方回应");
    }
    @ApiOperation(value = "根据id查询好友详细信息",notes = "")
    @GetMapping("/getFriendId/{friendId}")
    public CommonResponse getFriendId(@NotEmpty(message = "好友id不可为空") @PathVariable("friendId") Long friendId){
        UserInfoFriendDTO userInfoFriendDTO=friendService.getFriendInfo(friendId);
        return CommonResponse.buildSuccess(userInfoFriendDTO);
    }
    @ApiOperation(value = "删除好友",notes = "")
    @DeleteMapping("/removeFriendId/{friendId}")
    public CommonResponse removeFriendId(@NotEmpty(message = "好友id不可为空") @PathVariable("friendId") Long friendId){
        String response=friendService.removeFriendId(friendId);
        return CommonResponse.buildSuccess(response);
    }
    @ApiOperation(value = "根据id查询好友详细信息",notes = "")
    @GetMapping("/test/{friendId}")
    public CommonResponse test(@NotEmpty(message = "好友id不可为空") @PathVariable("friendId") Long friendId){
        Integer friend = friendImMapper.isFriend(UserUtils.getLoginUser().getUserInfoId(), friendId);
        return CommonResponse.buildSuccess(friend);
    }



}

